/**
 * @file capacitive_tp_hynitron_cst0xx.c
 * @descripttion:
 * @author: yabo.wang@transtekcorp.com
 * @date: 2020-08-18 18:22:53
 *
 * @copyright Copyright (c) 2020
 *
 */

#include "capacitive_tp_hynitron_cst0xx.h"
#include "ctp_hynitron_iic.h"
#include "ctp_hynitron_ext.h"
//#include "TpIicDriver.h"
#include "SEGGER_RTT.h"
#include "app_log.h"
#include "app_io.h"
#include "fat.h"
#include "oled_driver.h"
#include "../images/ui.h"

/*
*************************************************************************
*                              宏定义
*************************************************************************
*/

#define DBG_CTP

#ifdef DBG_CTP
    #define ctp_dbg_print(...)  _SEGGER_RTT_printf(__VA_ARGS__)
#else
    #define ctp_dbg_print(...)
#endif


/**
 * @brief   cst816t 初始化函数
 *
 */
uint16_t xpos = 0, ypos = 0;
char plane_press=0; 
char g_usb_linked=0;

extern int g_cnt_30time;
extern lv_obj_t * ui_Screen1_Label9;
extern lv_obj_t * ui_Screen1_Label7;
void app_io_event_handler(app_io_evt_t *p_evt)
{
    if (p_evt->pin == APP_IO_PIN_12)
    {
				
			  plane_press=1;
			  g_cnt_30time=0;
			  char buf[10];
		
			 // app_io_write_pin(APP_IO_TYPE_AON,TFT_PWM_PIN,APP_IO_PIN_SET);
			  if(g_screen_status==standby_test)
				{
					brightness_level(g_system_para.brightness);
//					lv_label_set_text(ui_Screen1_Label9, g_res_measure.timestamp);
//					if(g_res_measure.temp_res>200&g_res_measure.temp_res<450)
//					{
//						sprintf(buf,"%d.%d",g_res_measure.temp_res/10,g_res_measure.temp_res%10);
//						lv_label_set_text(ui_Screen1_Label7, buf);
//					}
//					else
//						lv_label_set_text(ui_Screen1_Label7, "--");
//					
//					if(g_res_measure.spo2_res==0)
//						lv_label_set_text(ui_Screen1_Label5, "--");
//					else
//					{
//						sprintf(buf,"%d",g_res_measure.spo2_res);
//						lv_label_set_text(ui_Screen1_Label5, buf);
//					}
//					
//					if(g_res_measure.hr_res==0)
//						lv_label_set_text(ui_Screen1_Label6, "--");
//					else
//					{
//						sprintf(buf,"%d",g_res_measure.hr_res);
//						lv_label_set_text(ui_Screen1_Label6, buf);
//					}
//					if(g_res_measure.hr_res==0)
//					lv_label_set_text(ui_Screen1_Label8, "--");
//					else
//					{
//						sprintf(buf,"%d",g_res_measure.hr_res/4);
//						lv_label_set_text(ui_Screen1_Label8, buf);
//					}	
					g_screen_status=idle;  
				}
				 if(g_screen_status==lowper_test)
				 {
					 	brightness_level(g_system_para.brightness);
				 }
				if(g_screen_status==charge_test||\
					g_screen_status==work_test)
				{
					brightness_level(g_system_para.brightness);
				
				}
			  
				ctp_hynitron_cst0_get_data(&xpos, &ypos);
				//APP_LOG_INFO("xpos = %d, ypos = %d\n", xpos, ypos);
    }
		 if (p_evt->pin == APP_IO_PIN_5)
    {
        
       // if (app_io_read_pin(APP_IO_TYPE_AON, APP_IO_PIN_5) == APP_IO_PIN_SET)
        {
            printf("\r\nKEY0 pressed.\r\n");
        }
    }
		if (p_evt->pin == APP_IO_PIN_6)
    {
			  if(app_io_read_pin(APP_IO_TYPE_AON, APP_IO_PIN_6) == APP_IO_PIN_RESET)
				{
					printf("usb is plugged.\r\n"); 
					//load charge ui
					screen_before=lv_scr_act();
					charge_init();
					g_usb_linked=1;		
					g_cnt_30time=0;					
					g_screen_status=charge_test;					
				}
				else if(app_io_read_pin(APP_IO_TYPE_AON, APP_IO_PIN_6) == APP_IO_PIN_SET)
				{
					printf("usb is removed.\r\n"); 
					//del charge ui
					g_usb_linked=0;
					CLOSE_SYS_POWER;
					//NVIC_SystemReset();  
					//lv_scr_load_anim(screen_before,LV_SCR_LOAD_ANIM_NONE, 10, 0,true);					
				}
				
    }
		
}
kal_bool ctp_hynitron_cst0_init(void)
{
#if CTP_HYNITRON_EXT == 1
    ctp_hynitron_update();
#endif
    hctp_i2c_init(CST0XX_I2C_SLAVEADDR, 300);

    hctp_reset_ic();

    hctp_delay_ms(200);

    kal_uint8 lvalue;
    hctp_read_bytes(0xA9, &lvalue, 1, 1);
	 // APP_LOG_INFO("tp id=%d\r\n",lvalue);
     
    /**读取TP版本号*/
//	extern uint32_t m_tp_cur_fw;
//	extern uint32_t m_tp_cur_cfg;

//    m_tp_cur_fw = lvalue;
//    m_tp_cur_cfg = lvalue;
	//配置中断
		uint16_t ret;
    app_io_init_t io_init = APP_IO_DEFAULT_CONFIG;

    io_init.pull = APP_IO_PULLUP;
    io_init.mode = APP_IO_MODE_IT_FALLING;
    io_init.pin  = APP_IO_PIN_12;
    io_init.mux  = APP_IO_MUX;

    ret = app_io_event_register_cb(APP_IO_TYPE_GPIOA, &io_init, app_io_event_handler, "KEY0 pin interrupt");
    if (ret != APP_DRV_SUCCESS)
    {
      //printf("APP_GPIO_KEY0 init failed.\r\n");
    }
	
    io_init.pull = APP_IO_PULLUP;
    io_init.mode = APP_IO_MODE_IT_BOTH_EDGE;
    io_init.pin  = APP_IO_PIN_6;
    io_init.mux  = APP_IO_MUX;

    ret = app_io_event_register_cb(APP_IO_TYPE_AON, &io_init, app_io_event_handler, "pin interrupt");
    if (ret != APP_DRV_SUCCESS)
    {
      printf("APP_GPIO_KEY0 init failed.\r\n");
    }	
	

    return CTP_TRUE;
}

/**
 * @brief   cst816t 设置电源模式
 *
 */
kal_bool ctp_hynitron_cst0_power_on(kal_bool enable)
{
    //_TODO:  Implement this funciton by customer
    if (enable)
    {
        CTP_SET_RESET_PIN_LOW;
        hctp_delay_ms(10);
        CTP_SET_RESET_PIN_HIGH;
    }
    else
    {
        kal_uint8 enterSleep[] = {0x03};
        hctp_write_bytes(0xE5, enterSleep, 1, 1);
    }
    return CTP_TRUE;
}

/*
This function is used to get the raw data of the fingures that are pressed.
When CTP IC send intterupt signal to BB chip, this function will be called in the interrupt handler function.

ATTENTION: Becasue this function is called in the interrupt handler function, it MUST NOT run too long.
That will block the entire system.
If blocking too long, it generally will cause system crash *....*
*/
kal_bool ctp_hynitron_cst0_get_data(kal_uint16 *xpos, kal_uint16 *ypos)
{
    kal_bool temp_result;
    kal_uint8 lvalue[5];
    kal_uint32 counter = 0;
    kal_uint32 model = 0;

    kal_bool ret = hctp_read_bytes(0x02, lvalue, 5, 1);

    if(ret == CTP_FALSE)
    {
       // APP_LOG_INFO("hctp_read_bytes error\n");
    }

    model = lvalue[0];
   // APP_LOG_INFO("ctp_get_data finger_num = %d\n", model);

    /*
    0 fingure meas UP EVENT, so return FALSE;
    And now we only support FIVE fingures at most, so if more than 5 fingures also return FALSE
    */
    if ((model == 0) || (model > 2))
    {
        return CTP_FALSE;
    }

    *xpos = (((kal_uint16)(lvalue[1] & 0x0f)) << 8) | lvalue[2];;
    *ypos = (((kal_uint16)(lvalue[3] & 0x0f)) << 8) | lvalue[4];
  //  APP_LOG_INFO("piont[%d], x:%d, y:%d\n", 0, *xpos, *ypos);
    return CTP_TRUE;
}

/**
 * @brief   cst816t 读点测试
 *
 */
void ctp_hynitron_cst0_test(void)
{
    //TpHalInit();
    ctp_hynitron_cst0_init();

    uint16_t xpos = 0, ypos = 0;
    while(1)
    {
        ctp_hynitron_cst0_get_data(&xpos, &ypos);
        //vTaskDelay(50);
			  delay_ms(100);
        APP_LOG_INFO("xpos = %d, ypos = %d\n", xpos, ypos);
    }
}
